import Vue from 'vue';
import Vuex from 'vuex';
Vue.use(Vuex);

// 需要维护的状态
const state = {
  adminType: 'none',
  info: {
    token: '',
    id: '',
    username: '',
    enterprise: '',
    user_type:'',
  },
  searchDrug:{
    id :'',
  }
};
const mutations = {
  admin(state, info) {
    state.info.token = info.token;
    state.info.id = info.id;
    state.info.enterprise = info.enterprise,
    state.info.username = info.username
    state.info.user_type = info.user_type
    // 在这里把用户名和token保存起来
    localStorage.setItem("currentUser_id", info.id);
    localStorage.setItem("currentUser_name", info.username);
    localStorage.setItem("currentUser_token", info.token);
    localStorage.setItem("currentUser_enterprise", info.enterprise);
    
  },
  super(state) {
    state.adminType = 'super';
  },
  ordinary(state) {
    state.adminType = 'ordinary';
  },
  setSearchDrug(state,info){
    state.searchDrug.id = info.id
  }
}
export default new Vuex.Store({
  state,
  mutations,
  getters: {
    getApp: (state) => state.adminType,
    getInfo: (state) => {
      if(localStorage.getItem('currentUser_name')){
        state.info.id = localStorage.getItem('currentUser_id')
        state.info.username = localStorage.getItem('currentUser_name')
        state.info.token = localStorage.getItem('currentUser_token')
        state.info.enterprise = localStorage.getItem('currentUser_enterprise')
      }
      return state.info
    },
    getSearchDrug: (state) => state.searchDrug
  }
});
